package com.cy.pj.sys.web;


import com.cy.pj.sys.service.impl.ServiceException;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalTime;

/**
 * SpringWEb模块定义的拦截器，其接口规范为HandlerInterceptor。
 * 当前
 */
public class TimeAccessInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("-=-=-=-=-=-=-");
        LocalTime localTime=LocalTime.now();//不包含年月日
        int hour=localTime.getHour();
        System.out.println("hour="+hour);
        if(hour<10||hour>18){
            throw new ServiceException("请在指定时间访问");
        }
        return true;//true为放行，false为请求到此结束
    }
}
